package com.example.eos.controller;

import com.example.eos.bean.GroupA;
import com.example.eos.bean.ReturnInfo;
import com.example.eos.bean.User;
import com.example.eos.mapper.UserMapper;
import com.example.eos.tool.GlobalParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class UserController {

    private UserMapper userMapper;

    @Autowired
    public UserController(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    //用户登录
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ReturnInfo userLogin(@RequestBody User user){
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(user.getPhone(),user.getPassword());
        Subject subject = SecurityUtils.getSubject();
        String message;
        try {
            subject.login(usernamePasswordToken);
            SecurityUtils.getSubject().getSession().setTimeout(-1000);
            userMapper.changeLasttimeByPhone(user.getPhone(), GlobalParam.getTime());
            message = "登录成功";
        }catch (Exception e){
            message = "帐号密码错误！";
        }
        return ReturnInfo.info1(message);
    }

    //用户退出
    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public void userLogout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }

    //查询用户
    @RequestMapping(value = "/user/{userid}",method = RequestMethod.GET)
    public ReturnInfo LookUser(@PathVariable String userid){
        String message;
        if(userMapper.findByUserid(userid)==null){
            message = "失败";
            return ReturnInfo.info2(message,"");
        }else {
            message = "成功";
            return ReturnInfo.info2(message,userMapper.findByUserid(userid));
        }
    }

    //新建用户
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public ReturnInfo CreatUser(@RequestBody @Validated({GroupA.class, Default.class}) User user, BindingResult result){
        String message = "失败";
        List<String> stringList = new ArrayList<>();
        if(result.hasErrors()){
            List<ObjectError> objectErrors = result.getAllErrors();
            for(ObjectError objectError : objectErrors){
                stringList.add(objectError.getDefaultMessage());
            }
            return ReturnInfo.info2(message, stringList);
        }else if(userMapper.findByPhone(user.getPhone())!=null){
            stringList.add("该手机号已注册");
            return ReturnInfo.info2(message, stringList);
        }else {
            message = "成功";
            userMapper.save(new User(user));
            return ReturnInfo.info2(message,"");
        }
    }

    //更新用户
    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public ReturnInfo UpdateUser(@RequestBody @Validated({Default.class}) User user, BindingResult result){
        String message = "成功";
        if(result.hasErrors()) {
            List<ObjectError> objectErrors = result.getAllErrors();
            message = "失败";
            return ReturnInfo.info2(message, objectErrors);
        }else {
            User userinfo =(User) SecurityUtils.getSubject().getPrincipal();
            user.setUserid(userinfo.getUserid());
            userMapper.update(user);
            return ReturnInfo.info2(message,"");
        }
    }

    //修改密码
    @RequestMapping(value = "/user/password", method = RequestMethod.PUT)
    public ReturnInfo UpdatePassword(@RequestBody User user){
        String message = "成功";
        User userinfo =(User) SecurityUtils.getSubject().getPrincipal();
        if(userMapper.findByUserid(userinfo.getUserid())==null){
            message = "失败";
        }else {
            user.setUserid(userinfo.getUserid());
            userMapper.updatePassword(user);
        }
        return ReturnInfo.info1(message);
    }

    //删除用户
    @RequestMapping(value = "/user/{userid}", method = RequestMethod.DELETE)
    public ReturnInfo DeteleUser(@PathVariable String userid){
        String message = "成功";
        if(userMapper.findByUserid(userid)==null){
            message = "失败";
        }else {
            userMapper.deleteByUserid(userid);
        }
        return ReturnInfo.info1(message);
    }

}
